package com.pj.android.brower.push

import android.content.Context
import android.content.Intent
import android.util.Log

import com.alibaba.fastjson.JSON
import com.pj.android.brower.push.bean.BasePushMsg
import com.pj.android.brower.push.bean.UmengPushMessage
import com.umeng.message.UmengMessageService

import org.android.agoo.common.AgooConstants
import org.json.JSONException
import org.json.JSONObject

class UmengNotificationService : UmengMessageService() {
    private val mPackageName = "com.pj.android.brower.push.bean"

    override fun onMessage(context: Context, intent: Intent) {
        val message = intent.getStringExtra(AgooConstants.MESSAGE_BODY)//消息内容

        Log.i("pj_log", "收到自定义消息: $message")
        val umengPushMessage = JSON.parseObject(message, UmengPushMessage::class.java)
        try {
            val obj = JSONObject(umengPushMessage.body)
            val custom = obj.getString("custom")
            val a = JSONObject(custom)
            val objType = Class.forName("$mPackageName.${a.getString("Type")}")
            val bodyMsg = JSON.parseObject(a.getString("Content"), objType) as BasePushMsg
            bodyMsg.excuteAction(this)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

}
